package com.hksj.aqm.util;

public class AQIUtil {

    public static double[] breakpoints = {0, 35, 75, 115, 150, 250, 350,500};
    public static double[] aqiValues = {0, 50, 100, 150, 200, 300, 400,500};

    /**
     * 根据 PM2.5 浓度计算对应的 AQI 值
     *
     * @param pm25 PM2.5 浓度值
     * @return 对应的 AQI 值
     */
    public static int calculateAQI_PM25(Double pm25) {
        return calculateAQI(pm25, breakpoints, aqiValues);
    }

    /**
     * 根据浓度值和分段点及对应 AQI 值计算 AQI 值
     *
     * @param concentration 浓度值(当天或24h的平均浓度值)
     * @param breakpoints   分段点数组
     * @param aqiValues    对应 AQI 值数组
     * @return 计算得到的 AQI 值
     */
    public static int calculateAQI(double concentration, double[] breakpoints, double[] aqiValues) {
        double result = 0;

        for (int i = 0; i < breakpoints.length - 1; i++) {
            if (concentration >= breakpoints[i] && concentration <= breakpoints[i + 1]) {
                result = ((aqiValues[i + 1] - aqiValues[i]) / (breakpoints[i + 1] - breakpoints[i])) * (concentration - breakpoints[i]) + aqiValues[i];
                break;
            }
        }
        return (int) Math.ceil(result);
    }

    /**
     * 获取健康关注程度
     * @param aqi
     * @return
     */
    public static Integer getPm25Level(Double pm25){
        Integer aqi = calculateAQI_PM25(pm25);
        return getLevel(aqi);
    }


    private static Integer getLevel(int aqi) {
        if (aqi >= 0 && aqi <= 50) {
            return 1;
        } else if (aqi >= 51 && aqi <= 100) {
            return 2;
        } else if (aqi >= 101 && aqi <= 150) {
            return 3;
        } else if (aqi >= 151 && aqi <= 200) {
            return 4;
        } else if (aqi >= 201 && aqi <= 300) {
            return 5;
        } else if (aqi >= 301 && aqi <= 500) {
            return 6;
        }
        return 0;
    }
    public static void main(String[] args) {
        int aqi_pm25 = calculateAQI_PM25(447.5);
        System.out.println(aqi_pm25);
    }

}
